﻿//SciChartPaneViewModel.cs
//Copyright (c) 2013 StockSharp LLC, all rights reserved.
//This code module is part of StockSharp library.
//This code is licensed under the GNU GENERAL PUBLIC LICENSE Version 3.
//See the file License.txt for the license details.
//More info on: http://stocksharp.com

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;

using Abt.Controls.SciChart;

using Ecng.Collections;

namespace StockSharp.Xaml
{
    partial class SciChartPaneViewModel : SciChartBaseViewModel, IChildPane
    {
        //private readonly ChartArea _area;

        private readonly Dictionary<IChartElement, SciChartElement> _elements =
            new Dictionary<IChartElement, SciChartElement>();

        private readonly SciChart _chart;
        private readonly SciChartViewModel _parentViewModel;

        private readonly ObservableCollection<IChartSeriesViewModel> _chartSeriesViewModels =
            new ObservableCollection<IChartSeriesViewModel>();

        public SciChartPaneViewModel(SciChartViewModel parentViewModel, ChartArea area, SciChart chart)
        {
            if (area == null) throw new ArgumentNullException("area");
            if (chart == null) throw new ArgumentNullException("chart");

            area.Elements.Added += AddSeries;
            area.Elements.Removing += RemoveSeries;
            area.Elements.Clearing += () => area.Elements.ForEach(RemoveSeries);

            _parentViewModel = parentViewModel;
            //_area = area;
            _chart = chart;

            area.Elements.ForEach(AddSeries);
        }

        public void ProcessValues(IDictionary<DateTime, IDictionary<IChartElement, object>> values)
        {
            foreach (var elems in values)
            {
                var time = elems.Key;

                foreach (var pair in elems.Value)
                {
                    var elem = pair.Key;

	                var scElem = _elements.TryGetValue(elem);

					if (scElem == null)
                        continue;

                    scElem.ProcessValue(time, pair.Value);
                }
            }
        }

        public void Reset(IEnumerable<IChartElement> elements)
        {
			foreach (var elem in elements)
            {
				var scElem = _elements.TryGetValue(elem);

				if (scElem == null)
					continue;

                scElem.Reset();
            }
        }

        private void AddSeries(IChartElement elem)
        {
            _chart.EnsureUIThread();

            if (_elements.ContainsKey(elem))
                throw new ArgumentException("duplicate chart element", "elem");

            var e = CreateElement(elem);
            _elements[elem] = e;

            if (e is SciChartCandles)
                ShowXAxis = true;

            UpdateLegend();
        }

        private void RemoveSeries(IChartElement elem)
        {
            _chart.EnsureUIThread();

			var scElem = _elements.TryGetValue(elem);

			if (scElem == null)
                throw new ArgumentException("no such element on the chart", "elem");

			scElem.OnRemoving();
            _elements.Remove(elem);

            UpdateLegend();
        }

        SciChartLegend _legend;

        private void UpdateLegend()
        {
            if (_legend == null)
            {
                _legend = new SciChartLegend
                {
                    HorizontalAlignment = HorizontalAlignment.Left,
                    VerticalAlignment = VerticalAlignment.Top,
                    CoordinateMode = AnnotationCoordinateMode.Relative,
                    X1 = 0,
                    Y1 = 0
                };
                Annotations.Add(_legend);
            }

            _legend.LegendItems.Clear();

            foreach (var e in _elements.Keys.OfType<ChartIndicatorElement>().Where(e => e.IsLegend))
            {
                _legend.LegendItems.Add(new LegendInfo(e.Title, e.Color));
            }
        }

        public string PaneGroup
        {
            get { return string.Format("ssharpscichart{0}", _chart.ChartId); }
        }

        public SciChartViewModel ParentViewModel
        {
            get { return _parentViewModel; }
        }

        public ObservableCollection<IChartSeriesViewModel> ChartSeriesViewModels
        {
            get { return _chartSeriesViewModels; }
        }

        private AnnotationCollection _annotations = new AnnotationCollection();

        public AnnotationCollection Annotations
        {
            get { return _annotations; }
            set { SetField(ref _annotations, value, "Annotations"); }
        }

        private string _yAxisTextFormatting;

        public string YAxisTextFormatting
        {
            get { return _yAxisTextFormatting; }
            set { SetField(ref _yAxisTextFormatting, value, "YAxisTextFormatting"); }
        }

        private string _title;

	    public string Title
        {
            get { return _title; }
            set { SetField(ref _title, value, "Title"); }
        }

        private bool _showXAxis;

        public bool ShowXAxis
        {
            get { return _showXAxis; }
            set { SetField(ref _showXAxis, value, "ShowXAxis"); }
        }

        private double _height = double.NaN;

        public double Height
        {
            get { return _height; }
            set { SetField(ref _height, value, "Height"); }
        }

		void IChildPane.ZoomExtents()
		{
			throw new NotImplementedException();
		}
    }
}